﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KEEPER_BRR_13dotNET.BusinessLogic;
using KP_CommonLibrary.Logs;
using KP_CommonLibrary.KP_CommonControls;
using KP_CommonLibrary;
using DevComponents.DotNetBar.SuperGrid;

namespace KEEPER_BRR_13dotNET.Presentation
{
    public partial class Ucl_ChuyenHoSo : KP_CommonLibrary.KP_CommonControls.KP_UserControl
    {
        public string MaViTri { get; set; }
        public string MaViTriNhan { get; set; }
        public string TenViTri { get; set; }
        public string TenViTriNhan { get; set; }
        public string MaTT { get; set; }
        public Ucl_ChuyenHoSo()
        {
            InitializeComponent();
        }
        void LoadHoSo()
        {
            try
            {
                BL_KPHoSo blHoSo = new BL_KPHoSo();
                MainGrid.SetDataSource(blHoSo.SelectByMaTtAndMaVt(MaTT, MaViTri));
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Lấy hồ sơ cần chuyển", ex);
                KPMessageBox.Show("Lấy hồ sơ cần chuyển:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }

        }
        private void Ucl_ChuyenHoSo_Load(object sender, EventArgs e)
        {
            LoadHoSo();
        }

        private void biRefresh_Click(object sender, EventArgs e)
        {
            LoadHoSo();
        }

        private void biChuyenHs_Click(object sender, EventArgs e)
        {

            List<GridRow> selectedRow = new List<GridRow>();
            for (int i = 0; i < MainGrid.PrimaryGrid.Rows.Count; i++)
            {
                GridRow row = MainGrid.PrimaryGrid.Rows[i] as GridRow;
                if (row.Checked)
                    selectedRow.Add(row);
            }
            if (selectedRow.Count == 0)
            {
                KPMessageBox.Show("Chưa chọn hồ sơ cần chuyển", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Warning);
                return;
            }
            Frm_LanChuyenEdit frm = new Frm_LanChuyenEdit();
            frm.NoiDung = string.Format("Chuyển {0} hồ sơ từ \"{1}\" sang \"{2}\"", selectedRow.Count, MaViTri, MaViTriNhan);
            DialogResult result = frm.ShowDialog();
            string noidung = frm.NoiDung;
            bool print = frm.InPhieuChuyen;
            frm.Dispose();
            if (result == DialogResult.OK)
            {
                try
                {
                    BL_KPChuyenHSo blChuyenHs = new BL_KPChuyenHSo();
                    string maChuyen = blChuyenHs.Insert(noidung, MaViTri, MaViTriNhan, selectedRow);
                    if (print)
                    {
                        //In phieu
                    }
                    KPMessageBox.Show("Đã chuyển thành công " + selectedRow.Count.ToString() + " hồ sơ");
                    foreach (GridRow node in selectedRow)
                        MainGrid.PrimaryGrid.Rows.Remove(node);
                }
                catch (Exception ex)
                {
                    LogUtil.Logger.Fatal("Chuyển hồ sơ", ex);
                    KPMessageBox.Show("Gặp lỗi khi chuyển hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

                }
            }

        }
    }
}
